﻿//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//

#include "pch.h"
#include "MainPage.xaml.h"
#include <ppltasks.h>

using namespace SimpleAudioPlayer;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Core;
using namespace Windows::Media;
using namespace Windows::Storage;
using namespace concurrency;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

MainPage::MainPage()
{
	InitializeComponent();

	// hook up app to system transport controls.
	systemControls = SystemMediaTransportControls::GetForCurrentView();
	systemControls->ButtonPressed += ref new TypedEventHandler<SystemMediaTransportControls^,
		SystemMediaTransportControlsButtonPressedEventArgs^>(this, &MainPage::OnButtonPressed);
	// to play audio in the background
	systemControls->IsPlayEnabled = true;
	systemControls->IsPauseEnabled = true;
}

void MainPage::OnSelected(Object^ sender, RoutedEventArgs^ e)
{
	auto picker = ref new Pickers::FileOpenPicker();
	picker->FileTypeFilter->Append(".mp3");
	create_task(picker->PickSingleFileAsync()).then([](StorageFile^ file)
	{
		if (file == nullptr)
			throw ref new OperationCanceledException();
		return file->OpenReadAsync();
	}).then([this](Streams::IRandomAccessStreamWithContentType^ stm)
	{
		_media->SetSource(stm, "");
	}).then([](task<void> t)
	{
		try
		{ 
			t.get();
		}
		catch (Exception^ ex)
		{}
	});
}


void MainPage::OnStop(Object^ sender, RoutedEventArgs^ e)
{
	_media->Stop();
}


void MainPage::OnPause(Object^ sender, RoutedEventArgs^ e)
{
	_media->Pause();
}


void MainPage::OnPlay(Object^ sender, RoutedEventArgs^ e)
{
	_media->Play();
}

void MainPage::OnButtonPressed(SystemMediaTransportControls^ sender,
	SystemMediaTransportControlsButtonPressedEventArgs^ e)
{
	switch (e->Button)
	{
	case SystemMediaTransportControlsButton::Play:
		Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
		{
			_media->Play();
		}));
		break;
	case SystemMediaTransportControlsButton::Pause:
		Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
		{
			_media->Pause();
		}));
		break;
	default:
		break;
	}
}

void MainPage::MusicChanged(Object^ sender, RoutedEventArgs^ e)
{
	switch (_media->CurrentState)
	{
	case MediaElementState::Playing:
		systemControls->PlaybackStatus = MediaPlaybackStatus::Playing;
		break;
	case MediaElementState::Paused:
		systemControls->PlaybackStatus = MediaPlaybackStatus::Paused;
		break;
	case MediaElementState::Stopped:
		systemControls->PlaybackStatus = MediaPlaybackStatus::Stopped;
		break;
	case MediaElementState::Closed:
		systemControls->PlaybackStatus = MediaPlaybackStatus::Closed;
		break;
	default:
		break;
	}
}